package com.isay.app.honey.ui.regist;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import com.isay.app.honey.R;
import com.isay.app.honey.manager.NetworkManager;
import com.isay.frameworklib.okhttp.ICommonCallback;
import com.isay.frameworklib.ui.BaseActivity;
import com.isay.frameworklib.user.UserManager;
import com.isay.frameworklib.utils.ToastUtils;

public class RegisterActivity extends BaseActivity implements View.OnClickListener {

    private EditText mEdtAccount;
    private EditText mEdtInviteCode;
    private EditText mEdtPwd;
    private EditText mEdtPwdSure;

    public static void launch(Context context) {
        Intent intent = new Intent();
        intent.setClass(context, RegisterActivity.class);
        context.startActivity(intent);
    }

    public static void launchForResult(Context context) {
        Intent intent = new Intent();
        intent.setClass(context, RegisterActivity.class);
        ((Activity) context).startActivityForResult(intent, 100);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_regist);
        initView();
    }

    private void initView() {
        findViewById(R.id.tv_title).setVisibility(View.GONE);
        findViewById(R.id.iv_go_back).setOnClickListener(this);
        findViewById(R.id.btn_register).setOnClickListener(this);
        mEdtAccount = findViewById(R.id.edt_account);
        mEdtInviteCode = findViewById(R.id.edt_invite_code);
        mEdtPwd = findViewById(R.id.edt_pwd);
        mEdtPwdSure = findViewById(R.id.edt_pwd_sure);
    }

    @Override
    protected int getStatusBarColor() {
        return 0;
    }

    @Override
    protected void onCreateActivity(Bundle savedInstanceState) {

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_go_back:
                finish();
                break;
            case R.id.btn_register:
                register();
                break;
            default:
                break;
        }
    }


    //点击注册
    private void register() {
        String username = mEdtAccount.getText().toString().trim();
        if (TextUtils.isEmpty(username)) {
            ToastUtils.show("账号不能为空");
            return;
        }
        String password = mEdtPwd.getText().toString().trim();
        if (TextUtils.isEmpty(password)) {
            ToastUtils.show("密码不能为空");
            return;
        }
        String repassword = mEdtPwdSure.getText().toString().trim();
        if (TextUtils.isEmpty(repassword)) {
            ToastUtils.show("密码不能为空");
            return;
        }
        sendRegister(username, mEdtInviteCode.getText().toString().trim(), password, repassword);
    }

    //发送注册信息
    private void sendRegister(final String username, String invitecode, final String password, String repassword) {
        showLoading("注册中");
        NetworkManager.register(username, invitecode, password, repassword, new ICommonCallback() {
            @Override
            public void onSuccess(int ret, String msg, String content) {
                if (ret == 200) {
                    UserManager.setUserName(username);
                    UserManager.setUserPwd(password);
                    Toast.makeText(RegisterActivity.this, msg, Toast.LENGTH_SHORT).show();
                    finish();
                } else {
                    ToastUtils.show(msg);
                }
                dismissDialog();
            }

            @Override
            public void onFailure(Exception e) {
                ToastUtils.show(e.toString());
                dismissDialog();
            }
        });
    }
}
